package com.bjpowernode.boss.mvc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjpowernode.boss.constants.BoosConst;
import com.bjpowernode.boss.convert.UserInfoConvert;
import com.bjpowernode.boss.domain.UserInfo;
import com.bjpowernode.boss.enums.ajax.Code;
import com.bjpowernode.boss.exception.BossException;
import com.bjpowernode.boss.mvc.model.dto.PageInfoDto;
import com.bjpowernode.boss.mvc.model.query.UserInfoQuery;
import com.bjpowernode.boss.mvc.model.result.AjaxResult;
import com.bjpowernode.boss.mvc.service.UserInfoService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private UserInfoConvert userInfoConvert;

    @PostMapping("edit")
    public AjaxResult saveOrEditUserByUser(@Validated @RequestBody UserInfoQuery userInfoQuery) {
        log.info("要编辑或添加的用户信息 : {} ===>  根据id确定是进行编辑 还是 更新操作" , userInfoQuery);
        userInfoService.saveOrEditUserByUser(userInfoQuery);
        return AjaxResult.ok();
    }


    @GetMapping("pageList")
    public AjaxResult selectUserInfoPageList(@RequestParam(defaultValue = "1") Integer pageNo) {
        log.info("要查询的是第  {}  页" , pageNo);
        IPage<UserInfo> pageResult = userInfoService.selectUserInfoPageList(pageNo , BoosConst.PAGE_SIZE);

        long current = pageResult.getCurrent(); // 当前是第几页
        long totalPage = pageResult.getPages();// 当前分页总页数
        // 分页数据信息
        PageInfoDto pageInfoDto = PageInfoDto.builder().current(current).totalPage(totalPage).build();
        // 分页查询数据
        List<UserInfo> userInfoList = pageResult.getRecords();
        // 封装数据
        Map<String , Object> data = new HashMap<>();
        data.put("page" , pageInfoDto);
        data.put("list", userInfoConvert.toConvertUserInfoVoList(userInfoList));
        return AjaxResult.ok(data);
    }

    @GetMapping("queryByNo")
    public AjaxResult loadUserInfoByUserNo(@RequestParam(required = false) String userNo) {
        log.info("用户信息的数据回调 : {}" , userNo);
        if (StrUtil.isBlank(userNo)) {
            throw new BossException(Code.REQUEST_PARAM_ERR);
        }
        UserInfo userInfo = userInfoService.loadUserInfoByUserNo(userNo);
        return AjaxResult.ok(userInfoConvert.toConvertLoadUserInfoEditVo(userInfo));
    }

    @PostMapping("editStatus")
    public AjaxResult editUserInfoStatusByUserNo(@RequestParam String userNo) {
        log.info("要修改状态的用户编号: {}" , userNo);
        if (StrUtil.isBlank(userNo)) {
            throw new BossException(Code.REQUEST_PARAM_ERR);
        }
        userInfoService.editUserInfoStatusByUserNo(userNo);
        return AjaxResult.ok();
    }

    @GetMapping("payConfig/matchFind")
    public AjaxResult matchFindUserInfoList(@RequestParam String shopName) {
        log.info("模糊查询用户信息 : {}" , shopName);
        if (StrUtil.isNotBlank(shopName) && shopName.length() >= 1) {
            List<UserInfo> matchUserInfoList = userInfoService.matchFindUserInfoList(shopName);
            return AjaxResult.ok(userInfoConvert.toConvertmuivList(matchUserInfoList));
        }
        return AjaxResult.fail(Code.REQUEST_PARAM_ERR);
    }
}
